// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#pragma once

#include <pollux/dwio/common/buffered_input.h>
#include <pollux/dwio/common/options.h>
#include <pollux/dwio/dwrf/common/decryption.h>
#include <pollux/dwio/dwrf/reader/reader_base.h>

namespace kumo::pollux::dwrf {

struct StripeMetadata {
  dwio::common::BufferedInput* stripeInput;
  std::shared_ptr<const StripeFooterWrapper> footer;
  std::unique_ptr<encryption::DecryptionHandler> decryptionHandler;
  StripeInformationWrapper stripeInfo;

  StripeMetadata(
      std::shared_ptr<dwio::common::BufferedInput> _stripeInput,
      std::shared_ptr<const proto::StripeFooter> _footer,
      std::unique_ptr<encryption::DecryptionHandler> _decryptionHandler,
      StripeInformationWrapper _stripeInfo)
      : StripeMetadata(
            std::move(_stripeInput),
            std::make_shared<StripeFooterWrapper>(_footer),
            std::move(_decryptionHandler),
            _stripeInfo) {}

  StripeMetadata(
      dwio::common::BufferedInput* _stripeInput,
      std::shared_ptr<const proto::StripeFooter> _footer,
      std::unique_ptr<encryption::DecryptionHandler> _decryptionHandler,
      StripeInformationWrapper _stripeInfo)
      : StripeMetadata(
            _stripeInput,
            std::make_shared<StripeFooterWrapper>(_footer),
            std::move(_decryptionHandler),
            _stripeInfo) {}

  StripeMetadata(
      std::shared_ptr<dwio::common::BufferedInput> _stripeInput,
      std::shared_ptr<const proto::orc::StripeFooter> _footer,
      std::unique_ptr<encryption::DecryptionHandler> _decryptionHandler,
      StripeInformationWrapper _stripeInfo)
      : StripeMetadata(
            std::move(_stripeInput),
            std::make_shared<StripeFooterWrapper>(_footer),
            std::move(_decryptionHandler),
            _stripeInfo) {}

  StripeMetadata(
      dwio::common::BufferedInput* _stripeInput,
      std::shared_ptr<const proto::orc::StripeFooter> _footer,
      std::unique_ptr<encryption::DecryptionHandler> _decryptionHandler,
      StripeInformationWrapper _stripeInfo)
      : StripeMetadata(
            _stripeInput,
            std::make_shared<StripeFooterWrapper>(_footer),
            std::move(_decryptionHandler),
            _stripeInfo) {}

 private:
  StripeMetadata(
      std::shared_ptr<dwio::common::BufferedInput> _stripeInput,
      std::shared_ptr<const StripeFooterWrapper> _footer,
      std::unique_ptr<encryption::DecryptionHandler> _decryptionHandler,
      StripeInformationWrapper _stripeInfo)
      : stripeInput{_stripeInput.get()},
        footer{std::move(_footer)},
        decryptionHandler{std::move(_decryptionHandler)},
        stripeInfo{std::move(_stripeInfo)},
        stripeInputOwned_{std::move(_stripeInput)} {}

  StripeMetadata(
      dwio::common::BufferedInput* _stripeInput,
      std::shared_ptr<const StripeFooterWrapper> _footer,
      std::unique_ptr<encryption::DecryptionHandler> _decryptionHandler,
      StripeInformationWrapper _stripeInfo)
      : stripeInput{_stripeInput},
        footer{std::move(_footer)},
        decryptionHandler{std::move(_decryptionHandler)},
        stripeInfo{std::move(_stripeInfo)} {}
  const std::shared_ptr<dwio::common::BufferedInput> stripeInputOwned_;
};

class StripeReaderBase {
 public:
  explicit StripeReaderBase(const std::shared_ptr<ReaderBase>& reader)
      : reader_{reader} {}

  virtual ~StripeReaderBase() = default;

  ReaderBase& getReader() const {
    return *reader_;
  }

  const std::shared_ptr<ReaderBase>& readerBaseShared() const {
    return reader_;
  }

  std::unique_ptr<const StripeMetadata> fetchStripe(
      uint32_t index,
      bool& preload) const;

 private:
  // stripeFooter default null arg should only be used for testing.
  void loadEncryptionKeys(
      uint32_t index,
      const proto::StripeFooter& stripeFooter,
      const StripeInformationWrapper& stripeInfo,
      encryption::DecryptionHandler& handler) const;

  const std::shared_ptr<ReaderBase> reader_;

  friend class StripeLoadKeysTest;
};

} // namespace kumo::pollux::dwrf
